package com.open.capacity.finance.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.open.capacity.finance.entity.SourceBasePay;
import com.open.capacity.finance.util.FileUtil;
import com.open.capacity.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.finance.entity.SourceBaseBudgetExcute;
import com.open.capacity.finance.dao.SourceBaseBudgetExcuteDao;
import com.open.capacity.finance.service.SourceBaseBudgetExcuteService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
public class SourceBaseBudgetExcuteServiceImpl implements SourceBaseBudgetExcuteService {

    @Autowired
    private SourceBaseBudgetExcuteDao sourceBaseBudgetExcuteDao;

    /**
     * 保存到数据库
     *
     * @param url
     */
    @Transactional
    public int save2db(String companyId, Integer kjnd, Integer importway, String url) {
        MultipartFile multipartFile = FileUtil.toMultipartFile("budgetexcutedataXls", url);
        ImportParams params = new ImportParams();
        params.setTitleRows(2); //前面header空几行
        params.setHeadRows(2);
        params.setStartRows(1);
        // 开启excel校验
        params.setNeedVerify(false);

        List<SourceBaseBudgetExcute> list = new ArrayList<>();
        try {
            list = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
                    SourceBaseBudgetExcute.class, params);
        } catch (Exception e) {
            log.error("从文件读取数据错误:{}", e.getMessage());
        }

        //删除旧数据
        if (importway == 1) {
            delete(companyId, kjnd);
        }

        for (SourceBaseBudgetExcute item : list) {
            if (item.getZbje() == null && item.getZbje1() == null) {
                continue;
            }
            if (StringUtil.isEmpty(item.getQuotaId())) {
                continue;
            }
            if (item.getZbje() == null && item.getZbje1() != null) {
                item.setZbje(item.getZbje1());
            }
            item.setCompanyId(companyId);
            item.setKjnd(kjnd);
            if (item.getZbje() != null && item.getZcs() != null && item.getZbje().compareTo(BigDecimal.ZERO) > 0) {
                item.setZcjd(item.getZcs().divide(item.getZbje(), BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP));
            }
            sourceBaseBudgetExcuteDao.save(item);
        }

        return list.size();
    }


    /**
     * 删除
     *
     * @param companyId
     */
    public int delete(String companyId, Integer kjnd) {
        return sourceBaseBudgetExcuteDao.deleteByCompanyIdAndKjnd(companyId, kjnd);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public SourceBaseBudgetExcute findById(String id) {
        return sourceBaseBudgetExcuteDao.findById(id);
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<SourceBaseBudgetExcute> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<SourceBaseBudgetExcute> list = sourceBaseBudgetExcuteDao.findAll(params);
        PageInfo<SourceBaseBudgetExcute> pageInfo = new PageInfo(list);

        return PageResult.<SourceBaseBudgetExcute>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<SourceBaseBudgetExcute> findAll(Map<String, Object> params) {
        return sourceBaseBudgetExcuteDao.findAll(params);
    }

}
